package net.zjitc.myqueue;


import net.zjitc.myqueue.exception.MyQuException;

import java.util.LinkedList;
/**
 * Created by Administrator on 2017/3/30.
 */
public class LinkedQu<E> implements MyQu<E>{
    private LinkedList<E> list = new LinkedList<>();
    private int maxLength ;

    /*写构造函数，定义队列的容纳的最大元素个数*/
    public LinkedQu(int maxLength) {
        this.maxLength = maxLength;
    }

    @Override
    public void add(E item) throws MyQuException {
        //最大元素个数确定后，在增加时需要判断队列是否为满
        if (isFull()){
            /*抛出异常*/
            throw  new  MyQuException("队列已经满了，不能增加了.....");
        }else {
            list.addLast(item);
        }
    }

    @Override
    public E remove() throws MyQuException {
        //在删除时需要判断队列是否为空
        if (isEmpty()){
            throw new MyQuException("队列为空，没能元素可以删除的.....");
        }
        return list.removeFirst();
    }

    @Override
    public E get() {
        return list.getFirst();
    }

    @Override
    public int size() {
        return list.size();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public boolean isFull() {
        if (list.size() == maxLength){
            return true;
        }
        return false;
    }
}
